package com.ma.chapter4.t8;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName OrderControl2
 * @Author: mayongqiang
 * @DATE 2022/3/16 19:58
 * @Description: 顺序模式  wait()
 */
@Slf4j(topic = "m.OrderControl2")
public class OrderControl2 {

    static ReentrantLock lock = new ReentrantLock();
    static Condition waitQueue = lock.newCondition();


    static boolean t2runned = false;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            try {
                lock.lock();
                while (!t2runned) {
                    waitQueue.await();
                }
                log.debug("1");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            try {
                lock.lock();
                log.debug("2");
                t2runned = true;
                waitQueue.signal();
            } finally {
                lock.unlock();
            }
        }, "t2");
        t1.start();
        t2.start();

    }

}
